<?php

namespace App\Http\Controllers;
use Notification;
use Illuminate\Http\Request;
use App\User;
use App\Notifications\ForgetPassword as f;
class ForgetPassword extends Controller
{
    /**
     * 加载邮箱地址
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
     public function siteEmail(){

            // 加载输入邮箱地址
            return view('ForgetPassword.siteEmail');
     }

    /**
     * 验证邮箱是否为我网站所有
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  sendEmail(Request $request){
         // dd($request);
          // 获取post数据中的email
          $post=$request->all();
          // dd($post);
          // 获取用户表数据里面的email用于来验证该用户是否为我的网站用户
          $user=User::where('email',$post['email'])->first();
          if($user){
              // 如果找到用户信息才能给他发送验证
              //参数解析:
              //第一个参数是通知消息发送给的用户数据对象,该数据对象中一定要有email字段
              //第二个参数是发送的消息通知类
              Notification::send($user,new f($user['email']));
              // dd($user['email']);die;
              session()->flash('success','邮件已发送您的邮箱，请进入邮箱里面进行操作！');
              return view('ForgetPassword.sendOK');
          }else{
              session()->flash('success','请输入正确邮箱地址！');
              return view('ForgetPassword.siteEmail');
          }
    }

    /**
     * 加载修改密码的模板
     * @param $email
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function changePassword($email){
          // dd($email);

         return view('ForgetPassword.password',compact('email'));
    }
    public function change(Request $request){
        $post = $request->all();
        $validator = $request->validate(
            [
                'password' => 'required|between:6,20|confirmed',
                'password_confirmation' => 'required|between:6,20',
            ]
        );
        //只有通过验证,才能进行下面的修改密码操作
        //通过获取的邮箱地址找到对应数据
        $user = User::where('email', $post['email'])->first();
        $user->password = bcrypt($post['password']);
        $user->save();
        //设置闪存信息并返回
        session()->flash('success','密码修改成功,请登录!!');
        return redirect()->route('login');
    }

}
